package cn.edu.jmu.oelp.controller;

import cn.edu.jmu.oelp.pojo.*;
import cn.edu.jmu.oelp.utils.FileUtils;
import cn.edu.jmu.oelp.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.*;

/**
 * @author huanghongbe
 */
@RestController
@Transactional(rollbackFor=Exception.class)
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/userInfo")
    public RespEntity userInfo(@RequestParam("userId")int userId){
        User user;
        user=userService.selUserById(userId);
        if (user!=null){
            return new RespEntity(RespCode.SUCCESS,user);
        }else {
            return new RespEntity(RespCode.WARN);
        }
    }

    @CrossOrigin
    @RequestMapping(value = "/uploadBase64", method = RequestMethod.POST)
        public RespEntity uploadBase64(@RequestBody UploadFile uploadFile) throws Exception {
        //String filePath = "/resources/static/headImage/";
        String filePath = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\headImage";
        //System.out.println(filePath);
        //System.out.println(uploadFile.getBase64());
        if (FileUtils.base64ToFile(filePath,uploadFile.getBase64(),uploadFile.getFilename())){
            System.out.println("储存成功");
            //将图片的访问路径传给前端
            String src=filePath+"\\"+uploadFile.getFilename();
            return new RespEntity(RespCode.SUCCESS,src);
        }else {
            System.out.println("储存失败");
            return new RespEntity(RespCode.WARN);
        }
    }

    @RequestMapping("/seeUser")
    public void seeUser(HttpServletRequest request)
    {
        User user = (User) request.getSession().getAttribute("user");
        System.out.println(user);
    }

    @PostMapping("/userLogin")
    public RespEntity userLogin(@RequestBody ReqLogin reqLogin){
//        if (userService.selUserByAccount(reqLogin.getAccount())==null){
//            return new RespEntity(RespCode.ACCOUNTERROR);
//        }
//        User user=new User();
//        user.setAccount(reqLogin.getAccount());
//        user.setPassword(reqLogin.getPassword());
//        user=userService.userLogin(user);
//        if (user!=null){
//            UUID uuid = UUID.randomUUID();
//            return new RespEntity(RespCode.LOGINSUCCESS,user,uuid.toString());
//        }else {
//            return new RespEntity(RespCode.PASSWORDERROR);
//        }
        String account= reqLogin.getAccount();
        String password= reqLogin.getPassword();
        String key="user:"+account+":password";

        User user=new User();
        String value = (String) redisController.redisGet(key);
        if(value!=null&&value.equals(password))
        {
            user.setAccount(account);
            user.setPassword(password);
            user=userService.userLogin(user);

            //UUID uid = UUID.fromString(user.getUsername()+"-"+user.getUsername()+"-"+user.getPassword()+"-"+user.getAccount()+"-"+user.getId());
            //System.out.println(uid);
            UUID uuid = UUID.randomUUID();
            return new RespEntity(RespCode.LOGINSUCCESS,user,uuid.toString());

        }else if(value==null&&userService.selUserByAccount(account)==null)
        {
            return new RespEntity(RespCode.ACCOUNTERROR);
        }
        else if(value==null&&userService.selUserByAccount(account)!=null)
        {
            user.setAccount(account);
            user.setPassword(password);
            user=userService.userLogin(user);
            if (user!=null){

                //UUID uid = UUID.fromString(user.getUsername()+"-"+user.getUsername()+"-"+user.getPassword()+"-"+user.getAccount()+"-"+user.getId());
                //System.out.println(uid);
                UUID uuid = UUID.randomUUID();

//            Map<String,Object> map = new HashMap<>();
//            map.put("data",user);
//            map.put("token",uid);
                redisController.redisSet(key,password);
                return new RespEntity(RespCode.LOGINSUCCESS,user,uuid.toString());
            }
            else {
                return new RespEntity(RespCode.PASSWORDERROR);
            }
        }

        return new RespEntity(RespCode.ACCOUNTERROR);
    }

    @Autowired
    RedisController redisController;
    @RequestMapping("/userLogin")
    public RespEntity userLogin(@RequestParam("account")String account, @RequestParam("password")String password, HttpServletRequest request){
        String key="user:"+account+":password";

        User user=new User();
        String value = (String) redisController.redisGet(key);
        if(value!=null&&value.equals(password))
        {
            user.setAccount(account);
            user.setPassword(password);
            user=userService.userLogin(user);
            request.getSession().setAttribute("user",user);
            //UUID uid = UUID.fromString(user.getUsername()+"-"+user.getUsername()+"-"+user.getPassword()+"-"+user.getAccount()+"-"+user.getId());
            //System.out.println(uid);
            UUID uuid = UUID.randomUUID();
            return new RespEntity(RespCode.LOGINSUCCESS,user,uuid.toString());

        }else if(value==null&&userService.selUserByAccount(account)==null)
        {
            return new RespEntity(RespCode.ACCOUNTERROR);
        }
        else if(value==null&&userService.selUserByAccount(account)!=null)
        {
            user.setAccount(account);
            user.setPassword(password);
            user=userService.userLogin(user);
            if (user!=null){
                request.getSession().setAttribute("user",user);
                //UUID uid = UUID.fromString(user.getUsername()+"-"+user.getUsername()+"-"+user.getPassword()+"-"+user.getAccount()+"-"+user.getId());
                //System.out.println(uid);
                UUID uuid = UUID.randomUUID();

//            Map<String,Object> map = new HashMap<>();
//            map.put("data",user);
//            map.put("token",uid);
                redisController.redisSet(key,password);
                return new RespEntity(RespCode.LOGINSUCCESS,user,uuid.toString());
            }
            else {
                return new RespEntity(RespCode.PASSWORDERROR);
            }
        }

        return new RespEntity(RespCode.ACCOUNTERROR);
    }
    @RequestMapping("/addUser")
    public RespEntity addUser(@RequestBody ReqUser reqUser,HttpServletRequest request){
        User user;
        if (reqUser!=null){
            //验证码比对
            String ycode = (String) request.getServletContext().getAttribute(reqUser.getPhoneNumber());
            if (ycode.equals(reqUser.getCode())) {
                if (userService.selUserByAccount(reqUser.getAccount()) != null) {
                    return new RespEntity(RespCode.USEREXIST);
                } else {
                    user = new User(reqUser.getAccount(), reqUser.getPassword(), reqUser.getUsername(), reqUser.getSrc(),reqUser.getPhoneNumber());
                    if (userService.addUser(user)) {
                        return new RespEntity(RespCode.REGISTSUCCESS, user);
                    }
                }
            }
        }
        return new RespEntity(RespCode.WARN);
    }


    @RequestMapping("/delUser")
    public RespEntity delUser(@RequestParam("id")int id) {
        User user=userService.selUserById(id);
        if (userService.delUser(id)){
            String key="user:"+user.getAccount()+":password";
            redisController.redisDel(key);
            return new RespEntity(RespCode.SUCCESS);
        }else {
            return new RespEntity(RespCode.WARN);
        }
    }

    @RequestMapping("/selUserByUserName")
    public RespEntity selUserByUserName(String username) {

        List<User> userList=userService.selUserByUserName(username);
        return new RespEntity(RespCode.SUCCESS,userList);
    }


    @RequestMapping("/selUserByAccount")
    public RespEntity selUserByAccount(String account) {
        User user=userService.selUserByAccount(account);
        return new RespEntity(RespCode.SUCCESS,user);
    }


    @RequestMapping("/selUserById")
    public RespEntity selUserById(int id) {
        User user=userService.selUserById(id);
        return new RespEntity(RespCode.SUCCESS,user);
    }


    @RequestMapping("/updateUser")
    public RespEntity updateUser(@RequestBody User user) {
        if (userService.updateUser(user)) {
           String account=user.getAccount();
           String value=user.getPassword();
           String key="user:"+account+":password";
           //更新redis账户和密码
           redisController.redisSet(key,value);
            return new RespEntity(RespCode.SUCCESS,user);
        } else {
            return new RespEntity(RespCode.WARN);
        }
    }

}
